<?php

namespace app\common\model\mysql;


use think\Model;

class BaseModel extends Model
{
    protected $pk = 'id';
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';

//    protected $autoWriteTimestamp = true;
//    protected function getCreateTimeAttr($value)
//    {
//        return date('Y-m-d H:i:s', $value);
//    }
//
//    protected function getUpdateTimeAttr($value)
//    {
//        return date('Y-m-d H:i:s', $value);
//    }

    // 获取单字段数据
    private function getAlone($where, $field = 'id')
    {
        return $this->where($where)->value($field);
    }

    // 获取单组数据
    private function getOne($where, $field = ['*'], $order = ['id' => 'DESC'])
    {
        return $this->field($field)->order($order)->where($where)->find();
    }

    // 获取单列数据
    private function getColumn($where, $field = 'id')
    {
        return $this->where($where)->column($field);
    }

    // 获取多条数据
    private function getList($where = [], $field = ['*'], $limit = [], $order = ['id' => 'DESC'])
    {
        $model = $this->field($field);
        if (!empty($where)) {
            $model = $model->where($where);
        }
        $model = $model->order($order);
        if (!empty($limit)) {
            $model = $model->limit($limit[0], $limit[1]);
        }
        return $model->select();
    }

    // 获取分页数据
    private function getPaginate($where = [], $field = '*', $paginate = 1, $order = ['id' => 'DESC'], $query = '')
    {
        $model = $this->field($field);
        if (!empty($where)) {
            $model = $model->where($where);
        }
        $model = $model->order($order);

        return $model->paginate(['list_rows' => $paginate, "query" => $query], false);

    }

    // 添加数据
    private function addData($data)
    {
        return $this->create($data);
    }

    // 批量添加数据，不会填充created_at 和 update_at
    private function addAll($data, $replace = false)
    {
        return $this->saveAll($data, $replace);
    }

    // 编辑数据
    private function editData($data, $where)
    {
        return $this->where($where)->update($data);
    }

    // 删除数据
    private function deleteData($where, $is_force = false, $soft_field = 'delete_time')
    {
        $model = $this->where($where);
        if ($is_force === false) {
            // 如果模型设置了软删除，而没有这句的话，则删除不生效
            // 如果模型没有设置软删除，而没有这句的话，就会直接删除
            $model->useSoftDelete($soft_field, time());
        }
        return $model->delete();
    }

    // 查询该数据是否存在
    private function checkRowExists($where, $filed = 'id')
    {
        return $this->where($where)->value($filed);
    }

    // 求总数
    private function getCount($where, $field = 'id')
    {
        return $this->where($where)->count($field);
    }

    // 求和
    private function getSum($where, $field = 'id')
    {
        return $this->where($where)->sum($field);
    }

    // 求最大值
    private function getMax($where, $field = 'id')
    {
        return $this->where($where)->max($field);
    }

    // 求最小值
    private function getMin($where, $field = 'id')
    {
        return $this->where($where)->min($field);
    }

    // 求平均值
    private function getAvg($where, $field = 'id')
    {
        return $this->where($where)->avg($field);
    }

    // 自增数据
    private function incValue($where, $field = 'id', $num = 1)
    {
        return $this->where($where)->inc($field, $num)->update();
    }

    // 自减数据
    private function decValue($where, $field = 'id', $num = 1)
    {
        return $this->where($where)->dec($field, $num)->update();
    }

    /**
     * 当静态调用model时
     * 如果存在类的方法，那么就会先判断方法是否静态的，不会调用__callStatic这个魔术方法
     * 如果不存在类的方法，则会直接调用这个方法
     * @param $name
     * @param $arg
     * @return mixed
     */
    public static function __callStatic($name, $arg)
    {
        return (new static)->$name(...$arg);
    }
}